/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * InitSystem.java
 *
 * 初始化系统，其实就是创建管理员帐户
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.tools;

import java.util.List;

import org.apache.log4j.Logger;

import com.sundy.core.Common;
import com.sundy.daosupport.TeacherTblDAO;
import com.sundy.service.TeacherTbl;

public class InitSystem {
	private static Logger logger = Logger.getLogger(InitSystem.class);

	public static boolean needCreateAdmin() {
		TeacherTblDAO dao = TeacherTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<TeacherTbl> list = dao.findAll();
		if (list == null || list.isEmpty())
			return true;
		else
			return false;
	}

	public static void main(String[] args) {
		if (args.length != 2) {
			System.out
					.println("Usage: com.sundy.tools.InitSystem manager-name password");
			System.exit(1);
		}
		TeacherTblDAO dao = TeacherTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<TeacherTbl> list = dao.findByName(args[0]);
		if (list != null && !list.isEmpty()) {
			System.out.println("User: " + args[0] + "has exists!");
			System.exit(1);
		}
		TeacherTbl user = new TeacherTbl(args[0], args[1], 1);
		user.setAdmin(1);
		dao.save(user);
		dao.getHibernateTemplate().flush();
		logger.info(String.format("%s create successful.", args[0]));
	}
}
